﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BLL;
using Model;

namespace GeniusWebApplication
{
    public partial class index : System.Web.UI.Page
    {
        public static User user = null;
        public static int categoryId = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["categoryId"] != null)
            {
                categoryId = int.Parse(Request["categoryId"]);
            }
            user = (User)HttpContext.Current.Session["user"];
            if (user == null)
            { Response.Redirect("log.aspx"); }

            //<span onmouseover="overZui(this);" onclick="clickNav(this);">企业文化</span>

            //初始化资源类型
            IList<Category> cl = new CategoryService().getCategoryList();

            foreach (Category c in cl)
            {
                this.libraryList.InnerHtml = this.libraryList.InnerHtml + "<span onmouseover='overZui(this);' onclick='clickNav(this);'><a href='./index.aspx?categoryId=" + c.id + "'>" + c.chineseName + "</a></span>";

            }

            //初始化最新文档
            

            
                IList<Resource> re = new ResourceService().getAllResources();
                if (re!=null)
                {
                for (int i = 0; i < (re.Count < 5 ? re.Count : 5); i++)
                {
                    if (re.ElementAt(i).status == 1)
                    {
                        if (re.ElementAt(i).type == 2)
                        {
                            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><a href='./item.aspx?resourceId=" + re.ElementAt(i).id + "'><p class='hide'>" + re.ElementAt(i).fileName + "</p><p>      ----" + re.ElementAt(i).uploadTime + "</p></a></div>";
                        }
                        else if (re.ElementAt(i).type == 1)
                        {
                            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><a href='./item_media.aspx?resourceId=" + re.ElementAt(i).id + "'><p class='hide'>" + re.ElementAt(i).fileName + "</p><p>      ----" + re.ElementAt(i).uploadTime + "</p></a></div>";
                        }
                        else if (re.ElementAt(i).type == 3)
                        {
                            this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><a href='./item_else.aspx?resourceId=" + re.ElementAt(i).id + "'><p class='hide'>" + re.ElementAt(i).fileName + "</p><p>      ----" + re.ElementAt(i).uploadTime + "</p></a></div>";
                        
                        }
                    }
                    //this.order.InnerHtml = this.order.InnerHtml + "<div class='ordersItem'><p class='hide'>" + re.ElementAt(i).fileName+ "</p><p>      ----" + re.ElementAt(i).uploadTime + "</p></div>";
                }
            }



            //初始化最热资源
            ResourceService resourceS = new ResourceService();

            if (resourceS.getResourceByCategory(categoryId) != null)
            {
                IList<Resource> rl = new ResourceService().getResourceByCategory(categoryId);
                FavoriteService fss=new FavoriteService();
                foreach (Resource c in rl)
                {
                    int i = c.marks / c.marksUserNum;
                    if (c.status == 1)
                    {
                        if (c.type == 2)
                        {
                            if (fss.getFavourite(user, c) == null)
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j<5;j++ )
                                {
                                    if(j<i){
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                                
                            }
                            else
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j < 5; j++)
                                {
                                    if (j < i)
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                            }
                        }
                        else if (c.type == 1)
                        {
                            if (fss.getFavourite(user, c) == null)
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j < 5; j++)
                                {
                                    if (j < i)
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item_media.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";

                            }
                            else
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j < 5; j++)
                                {
                                    if (j < i)
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item_media.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                            }
                        }
                        else if (c.type == 3)
                        {
                            if (fss.getFavourite(user, c) == null)
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j < 5; j++)
                                {
                                    if (j < i)
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item_else.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_star.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";

                            }
                            else
                            {
                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<li><span class='myListUl-in'></span><span>";

                                for (int j = 0; j < 5; j++)
                                {
                                    if (j < i)
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_fab.png'>";
                                    }
                                    else
                                    {
                                        this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<img src='./img/opt_star.png'>";
                                    }
                                }


                                this.myListUl.InnerHtml = this.myListUl.InnerHtml + "</span><span ><a href='./item_else.aspx?resourceId=" + c.id + "'>" + c.fileName + "</a></span><span class='myListUl-text'><span id='myListUl-img1' ><a href='./download.aspx?resourceId=" + c.id + "'><img src='./img/opt_dow.png' title='下载'></a></span><span id='myListUl-img2' ><img src='./img/opt_fab.png' title='收藏' onclick='clickFab(this);' id='" + c.id + "'></span></span> </li>";
                            }
                        }
                    }
                }
                //this.myListUl.InnerHtml = this.myListUl.InnerHtml + "<><>";
            }

            //初始化关键字
            IList<Keyword> kl = new KeywordService().getKeywords();
            List<string> keyStringList = new List<string>();
            List<int> keyStringListBig = new List<int>();
            foreach (Keyword k in kl)
            {

                if (keyStringList.Count() == 0)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else if (keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; }) == -1)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else
                {
                    int ii = keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; });
                    if (keyStringListBig[ii] >= 2)
                    {
                        keyStringListBig[ii] = keyStringListBig[ii] - 1;
                    }
                }
            }

            for (int m = 0; m < keyStringList.Count(); m++)
            {
                this.keys.InnerHtml = this.keys.InnerHtml + "<div class='keysItem'><p class='ggz" + keyStringListBig[m] + "'><a href='./keywords.aspx?keys=" + keyStringList[m] + "'>" + keyStringList[m] + "</a></p></div>";
            }

        }

        [WebMethod]
        public static string ajaxSaveFab(int resourceId)
        {

            FavoriteService fService = new FavoriteService();

            Hashtable ht = fService.addFavourite(user, resourceId);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";

        }
        [WebMethod]
        public static string ajaxDelFab(int resourceId)
        {

            FavoriteService fService = new FavoriteService();
            IList<Favourite> f = fService.getUserFavourite(user.id);
            Hashtable ht = new Hashtable();
            foreach (Favourite mf in f)
            {
                if (mf.resource.id == resourceId)
                {
                    ht = fService.delFavourite(mf.id);
                }
            }
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";

        }

        [WebMethod]
        public static string ajaxSearchResource(int searchType, string searchText)
        {
            Hashtable ht = new ResourceService().searchResource(searchText, searchType);
            ht.Add("searchText", searchText);
            HttpContext.Current.Session.Add("researHT", ht);
            return "{" +
                        "result:'" + "1" + "'," +
                        "message:'" + "搜索完毕" + "'" +
                    "}";
        }
    }
}